package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * 商机跟进记录查询
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> getbusinessTrackRecord(Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = tbBusinessTrackRecordMapper.getbusinessTrackRecord(id);
        tbBusinessTrackRecords.forEach(s->{
            if (s.getKeyItems()!=null && s.getKeyItems()!=""){
            String[] dictValue = s.getKeyItems().split(",");
            List<String> dictLabel = new ArrayList<>();

                for (int i = 0; i < dictValue.length; i++) {
                    String point = sysDictDataMapper.selectDictLabel("communication_point", dictValue[i]);
                    dictLabel.add(point);
                }
                s.setKeys(dictLabel);
            }

        });
        return tbBusinessTrackRecords;
    }

    /**
     * 新增跟进记录
     * @param businessTrackVo
     */
    @Transactional
    @Override
    public void addBusinessRecord(BusinessTrackVo businessTrackVo) {
        //获取businessId
        Long businessId = businessTrackVo.getBusinessId();
        //获取跟进记录
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        tbBusinessTrackRecord.setBusinessId(businessId);
        //获取创建人
        TbBusiness tbBusiness1 = tbBusinessMapper.selectTbBusinessById(businessId);
        tbBusinessTrackRecord.setCreateBy(tbBusiness1.getCreateBy());
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord,new String[]{"id"});
        tbBusinessTrackRecordMapper.insertBusinessTrackRecord(tbBusinessTrackRecord);
        //修改商机信息
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }
}
